// JavaScript Document
$(function () {
	initModify();
	//提交
	$(".submit").on("click",function(){
		checkModify();
	});
	//图片文件内容改变
	$(".uploadFile").on("change",function(){
		$(".imgurl").val($(this).val());
        var reader = new FileReader();
        reader.readAsDataURL(this.files[0]);
        reader.onload = function(e){
            var strbase64 = e.target.result;
            var a = strbase64.split("base64,");
            $('.file_base64').val(a[1]);
        };
        preImg();//显示图片
	});
	//点击浏览图片
	$(".scan").on("click",function(){
		$(".uploadFile").click();	
	});
});
//初始化
function initModify(){
	getInfo(getQueryString("id"));	
}
//获取详情
function getInfo(id){
	var str = 'crmbrUuid='+encodeURIComponent(id);
	getOData(str,"coreMyButler/views",{fn:function(oData){
		if(oData.code == 1) {
			$(".crmbrName").val(oData.data.crmbrName || "");
			$(".crmbrTel").val(oData.data.crmbrTel || "");
			$(".crmbrQq").val(oData.data.crmbrQq || "");
			if(oData.data.list != undefined){
				var imgUrl="/"+oData.data.list[0].cratmDir+"/"+oData.data.list[0].cratmFileName;
				$(".imgurl").val(imgUrl || "");
				getImageWidthHeight(urlfile+"/coreAttachment/image/get/thumb/"+oData.data.list[0].cratmUuid,function(realWidth,realHeight){
					var width = 0;
					var height = 200;
					//如果真实的宽度大于浏览器的宽度就按照200显示
					if(realHeight>=height){
						width = realWidth/realHeight*height;
						$(".preimg").css("width",width).css("height",height);
					}
					else{//如果小于浏览器的宽度按照原尺寸显示
						$(".preimg").css("width",realWidth+'px').css("height",realHeight+'px');
					}
					$(".preimg").attr("src",urlfile+"/coreAttachment/image/get/thumb/"+oData.data.list[0].cratmUuid);
					$(".preimg").attr("data-filename",imgUrl);
				});
			}
		} else {
			alert(data.errMsg);
		}
	}});
}
//检查提交
function checkModify(){
	if($.trim($(".crmbrName").val()) == ""){
		alert("名字不能为空，请选择再提交！");
		$(".crmbrName").focus();
		return false;
	}
	if($.trim($(".crmbrTel").val()) == ""){
		alert("电话号码不能为空，请填写完再提交！");
		$(".crmbrTel").focus();
		return false;
	}
	if($.trim($(".crmbrQq").val()) == ""){
		alert("QQ不能为空，请填写完再提交！");
		$(".crmbrQq").focus();
		return false;
	}
	if($.trim($(".imgurl").val()) == ""){
		alert("附件不能为空，请上传附件再提交！");
		return false;
	}
	
	var r=confirm("是否确认修改？");
	if (r==true){
		var msgObject = parent.layer.msg('处理中，请等待……', {
			icon: 16,
			shade: 0.4,
			time: waitImgTime //（如果不配置，默认是3秒）
		}, function(index){
			//do something
			parent.layer.close(index);
		});
		if($.trim($(".imgurl").val()) == $(".preimg").attr("data-filename")){
			Modify(msgObject);
		}else{
			uploadPic(getQueryString("id"),msgObject);
		}
	}
}

//上传图片
function uploadPic(cratmBusUuid,msgObject){
	$(".cratmBusUuid").val(cratmBusUuid);
	$("#userForm").ajaxSubmit({
        //定义返回JSON数据，还包括xml和script格式
        type: "POST",
        dataType:'json',
        url: urlfile + "coreAttachment/upload/update",
        success: function(strdata) {
        	if(strdata.success==true){
        		Modify(msgObject);
        	}else {
            	alert(strdata.errMsg);
            }
        }
    });
}

//提交
function Modify(msgObject){
	var crmbrUuid = getQueryString("id");
	var crmbrName = $(".crmbrName").val();
	var crmbrTel = $(".crmbrTel").val();
	var crmbrQq = $(".crmbrQq").val();
	var str = 'crmbrUuid='+encodeURIComponent(crmbrUuid)+'&crmbrName='+encodeURIComponent(crmbrName)+'&crmbrTel='+encodeURIComponent(crmbrTel)+'&crmbrQq='+encodeURIComponent(crmbrQq);
	getOData(str,"coreMyButler/update/coreMyButler",{
		fn:function(oData){
			window.parent.refreshList();
			alert("修改成功！");
		},
		fnerr:function(oData){
			parent.layer.close(msgObject);
		}
		
	});
}